/*
 * ******************************************************************************
 *   Copyright (c) 2013-2014 Gabriele Mariotti.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *  *****************************************************************************
 */

package it.gmariotti.cardslib.demo.fragment.v1;

import it.gmariotti.cardslib.demo.ResourceTable;
import it.gmariotti.cardslib.demo.cards.MayKnowCard;
import it.gmariotti.cardslib.library.Constants;
import it.gmariotti.cardslib.library.internal.Card;
import it.gmariotti.cardslib.library.internal.CardArrayAdapter;
import it.gmariotti.cardslib.library.internal.CardHeader;
import it.gmariotti.cardslib.library.utils.PixelMapUtils;
import it.gmariotti.cardslib.library.view.CardListView;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;

import java.util.ArrayList;
import java.util.List;

/**
 * List base example wuth different inner layouts
 *
 * @author Gabriele Mariotti (gabri.mariotti@gmail.com)
 */
public class ListDifferentInnerBaseFragment extends MaterialV1Fragment {

    protected ScrollView mScrollView;

    @Override
    protected int getSubTitleHeaderResourceId() {
        return ResourceTable.String_header_title_subtitle_ldiff;
    }

    @Override
    protected int getTitleHeaderResourceId() {
        return ResourceTable.String_header_title_group3;
    }

    @Override
    protected String getDocUrl() {
        return "";
    }

    @Override
    protected String getSourceUrl() {
        return "";
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_demo_fragment_list_base_different_inner);
        initCards();
    }


    private void initCards() {

        ArrayList<Card> cards = new ArrayList<Card>();
        for (int i = 0; i < 50; i++) {

            CardExample card = new CardExample(this, "My title " + i, "Inner text " + i);
            cards.add(card);

            CardExample2 cardx = new CardExample2(getContext());
            cardx.title = "Application example " + i;
            cardx.secondaryTitle = "A company inc..." + i;
            cardx.rating = (float) (Math.random() * (5.0));
            cardx.count = i;
            cards.add(cardx);

            MayKnowCard card2 = new MayKnowCard(getContext());
            card2.setType(2); //Very important with different inner layout
            card2.setSwipeable(true);
            cards.add(card2);

        }

        // Provide a custom adapter.
        // It is important to set the viewTypeCount
        // You have to provide in your card the type value with {@link Card#setType(int)} method.
        CardArrayAdapter mCardArrayAdapter = new CardArrayAdapter(this, cards);
        mCardArrayAdapter.setInnerViewTypeCount(3);

        // An alternative is to write a own CardArrayAdapter
        // MyCardArrayAdapter mCardArrayAdapter = new MyCardArrayAdapter(this,cards);


        CardListView listView = (CardListView) this.findComponentById(ResourceTable.Id_carddemo_list_base1);
        if (listView != null) {
            listView.setAdapter(mCardArrayAdapter);
        }
    }

    /**
     * With multiple inner layouts you have to set the viewTypeCount with {@link CardArrayAdapter#setInnerViewTypeCount(int)}.
     * </p>
     * An alternative is to provide your CardArrayAdapter  where you have to override this method:
     * </p>
     * public int getViewTypeCount() {}
     * </p>
     * You have to provide in your card the type value with {@link Card#setType(int)} method.
     */
    public class MyCardArrayAdapter extends CardArrayAdapter {

        /**
         * Constructor
         *
         * @param context The current context.
         * @param cards   The cards to represent in the ListView.
         */
        public MyCardArrayAdapter(Context context, List<Card> cards) {
            super(context, cards);
        }

        @Override
        public int getComponentTypeCount() {
            return 3;
        }
    }

    //--------------------------------------------------------------------------
    // Cards
    //--------------------------------------------------------------------------

    public class CardExample extends Card {

        protected String mTitleHeader;
        protected String mTitleMain;

        public CardExample(Context context, String titleHeader, String titleMain) {
            super(context, ResourceTable.Layout_carddemo_example_inner_content);
            this.mTitleHeader = titleHeader;
            this.mTitleMain = titleMain;
            init();
        }

        private void init() {

            //Create a CardHeader
            CardHeader header = new CardHeader(getContext());

            //Set the header title
            header.setTitle(mTitleHeader);

            //Add a popup menu. This method set OverFlow button to visible
            //todo
//            header.setPopupMenu(R.menu.popupmain, new CardHeader.OnClickCardHeaderPopupMenuListener() {
//                @Override
//                public void onMenuItemClick(BaseCard card, MenuItem item) {
//                    Toast.makeText(this, "Click on card menu" + mTitleHeader +" item=" +  item.getTitle(), Toast.LENGTH_SHORT).show();
//                }
//            });
            addCardHeader(header);

            //Add ClickListener
            setClickedListener((card, view) -> new ToastDialog(getContext()).setText("Click Listener card=" + mTitleHeader).show());

            //Set the card inner text
            setTitle(mTitleMain);

            setSwipeable(true);
        }


        @Override
        public int getType() {
            //Very important with different inner layouts
            return 0;
        }
    }

    public class CardExample2 extends Card {

        protected Text mTitle;
        protected Text mSecondaryTitle;
        protected Rating mRatingBar;
        protected int resourceIdThumbnail;
        protected int count;

        protected String title;
        protected String secondaryTitle;
        protected float rating;


        public CardExample2(Context context) {
            super(context, ResourceTable.Layout_carddemo_mycard_inner_content);
            init();
        }

        private void init() {

            //Create a CardHeader
            CardHeader header = new CardHeader(getContext());
            addCardHeader(header);

            //Add ClickListener
            setClickedListener((card, view) -> new ToastDialog(getContext()).setText("Click Listener card=" + getTitle()).show());

            setSwipeable(true);
        }


        @Override
        public void setupInnerViewElements(ComponentContainer parent, Component view) {

            //Retrieve elements
            mTitle = (Text) parent.findComponentById(ResourceTable.Id_carddemo_myapps_main_inner_title);
            mSecondaryTitle = (Text) parent.findComponentById(ResourceTable.Id_carddemo_myapps_main_inner_secondaryTitle);
            mRatingBar = (Rating) parent.findComponentById(ResourceTable.Id_carddemo_myapps_main_inner_ratingBar);

            if (mTitle != null)
                mTitle.setText(title);

            if (mSecondaryTitle != null)
                mSecondaryTitle.setText(secondaryTitle);

            if (mRatingBar != null) {
                try {
                    mRatingBar.setFilledElement(PixelMapUtils.prepareElementById(getContext(), ResourceTable.Media_rate_star_small_on_holo_dark));
                    mRatingBar.setUnfilledElement(PixelMapUtils.prepareElementById(getContext(), ResourceTable.Media_rate_star_small_off_holo_dark));
                    mRatingBar.setHalfFilledElement(PixelMapUtils.prepareElementById(getContext(), ResourceTable.Media_rate_star_small_half_holo_dark));
                    mRatingBar.setRatingItems(5);
                    mRatingBar.setMaxValue(5);
                    mRatingBar.setGrainSize(0.5f);
                    mRatingBar.setScore(rating);
                } catch (Exception e) {
                    HiLog.error(Constants.LABEL_LOG, e.toString());
                }
            }
        }

        @Override
        public int getType() {
            //Very important with different inner layouts
            return 1;
        }
    }


}
